<!DOCTYPE html>
<html>
<head>
<title>组织机构管理</title>
<link rel="stylesheet" href="layuiadmin/style/admin.css" media="all">
<link rel="stylesheet" href="js/zTree/css/zTreeStyle/zTreeStyle.css" media="all">
<script type="text/javascript" src="js/zTree/js/jquery.ztree.all.min.js"></script>
</head>
<body>
<div class="layui-fluid">
  <div class="layui-card">  
    <div class="layui-card-body">
      <div class="layui-row">
    <div class="layui-col-md2">
       <ul id="treeDemo" class="ztree"></ul>
    </div>
    <div class="layui-col-md10 layui-col-space2">
	    <div class="layui-row">
		    <div class="layui-inline">
		         <input type="text" id="name" placeholder="机构名称" autocomplete="off" class="layui-input">
		    </div>
		    <div class="layui-btn-group">
			    <button type="button" class="layui-btn layui-btn-primary" data-type="query">查询</button>
			    <button type="button" class="layui-btn" data-type="add">增加</button>
			    <button type="button" class="layui-btn" data-type="refreshPersonNum">刷新人数</button>
			    <button type="button" class="layui-btn" data-type="move">移动</button>
			    <button type="button" class="layui-btn" data-type="moveTop">移动到顶部</button>
			    <button type="button" class="layui-btn" data-type="batchDel">删除</button>
		  	</div>
	  	</div>
	 	<div class="layui-row">
	      <table id="org-table" lay-filter="org-table"></table>
	    </div>
    </div>
  	</div>
    </div>
  </div>
</div>

<script th:inline='none'>

	layui.use(['table', 'tree','form'], function(){
	var form = layui.form
	,table = layui.table;
	
	var treeSetting = {
		async: {
			enable: true,
			url: "org/getOrgTree",
			autoParam:["id=pid"]
		},
		view:{
			showTitle: true,
			selectedMulti : false
		},callback:{
			onClick: function(event,treeId,node){
				var whr = {parentId:node.id};
				table.reload('org-table',{
					loading:true,
					where: whr
					,page: {
						 curr: 1 //重新从第 1 页开始
					}
				});				
			}
		}
	};
	$.fn.zTree.init($("#treeDemo"), treeSetting);
	var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
	table.render({
		elem: '#org-table'
		,url: 'org/getPageList' 
		,cols: [[
		{type: 'checkbox'}
		,{field: 'id', width: 100, title: 'ID',hide:true}
		,{field: 'parentId', width: 100, title: '父ID',hide:true}
		,{field: 'orderno', title: '排序',edit:'text', width: 100}
		,{field: 'orgCode', title: '机构编号', width:100 ,hide:true}
		,{field: 'name', title: '机构名称', width:200,templet:function(d){
	      	return "<a class='colum-link' href='javascript:void(0)' onclick='openDetail(this,\""+d.id+"\")'>" + d.name + "</a>";
	     }}
		,{field: 'shortName', title: '机构简称', width: 170}
		,{field: 'orgTypeTxt', title: '机构类型', width: 140}
		,{field: 'tier', title: '层级', width: 140}
		
		]]
		,page: true
		,limit: 30
		,height: top.height - 100
		,text: '对不起，加载出现异常！'
		,done:function(){
			enableRowClick(table,form,'org-table',"org-table");
		}	
	});
	table.on("edit(org-table)",function(obj){
		$.ajax({
			url:'org/modify',
			type:'POST',
			data:obj.data,
			dataType:'json',
			success:function(ret){
				layer.msg("保存成功",{icon:1});
				refreshNode();
				table.reload('org-table'); 
			}
		})
	})
	window.loadGrid = function(id){
		var checkStatus = "";
		if(typeof(id)=="undefined"){
			checkStatus = table.checkStatus('org-table');
			checkData = checkStatus.data; 
		}else{
			checkData = id;
		}
		table.reload('org-table');
		selectedRows(checkData,table,form,'org-table','org-table');
	}
		function refreshNode(){
			var selectedNodes = treeObj.getSelectedNodes();
		
			if( selectedNodes.length==0){
				treeObj.reAsyncChildNodes(null, "refresh");
			}else{
				var org = getEntityInfo(selectedNodes[0].id,"org/getById");
				if(typeof(org.id)=="undefined"){
					treeObj.removeNode(selectedNodes[0]);
					return;
				}
			
				var count = getSigleValueFromServer({parentId:selectedNodes[0].id},"org/countNodes");
				if(count==1&&selectedNodes[0].isParent==false){
				selectedNodes[0].zAsync = false;
				selectedNodes[0].isParent = true;
				}else if(count==0){
				selectedNodes[0].zAsync = true;
				selectedNodes[0].isParent = false;
				treeObj.updateNode(selectedNodes[0]);
				}
			
				treeObj.reAsyncChildNodes(selectedNodes[0], "refresh");
			
		}
	}
	window.refreshNode = refreshNode;
	//事件
	var action = {
		query:function(){
			var whr = {name:$('#name').val(),orgId:""};	 
			table.reload("org-table",{where:whr}) 
		},
		batchDel: function(){
		var checkStatus = table.checkStatus('org-table')
		,checkData = checkStatus.data; //得到选中的数据
		var ids = [];
		for(let i=0;i<checkData.length;i++){
			ids[i] = checkData[i].id;
		}
		if(checkData.length === 0){
			return layer.msg('请选择要删除的记录');
		}
		layer.confirm('确定删除吗？', function(index) {
			$.ajax({
				url:"org/delete",
				type:'POST',
				dataType:'json',
				data:{ids: ids},
				success:function(ret){
					table.reload('org-table');
					refreshNode();
					layer.msg('已删除');
				}
			})
			
			})
		}
		,add: function(){
			var selectedNodes = treeObj.getSelectedNodes();
			/* if(selectedNodes.length==0){
				return layer.msg("请选择父机构",{icon:0});
			} */
			var selectedId = "";
			if(selectedNodes.length==1){
				selectedId = selectedNodes[0].id;
			}
			layer.open({
				type: 2,
				title:'增加机构',
				area:['1000px','700px'],
				content:  'rooter?url=system/org/add-org&pid='+selectedId,
				btn:['保存','关闭'],
				yes:function(index,layero){
					var dataForm = layer.getChildFrame('form', index);
					dataForm.find("#submitBtn").click();
					return false
				}
			});    
		},edit: function(){
			var checkStatus = table.checkStatus('org-table')
			,checkData = checkStatus.data; 
			
			if(checkData.length === 0){
			return layer.msg('请选择要修改的公司记录');
			}
			layer.open({
				type: 2,
				title:'修改公司信息',
				area:['800px','700px'],
				content: 'rooter?url=system/org/add-org&id='+checkData[0].id,
				btn:['保存','关闭'],
				yes:function(index,layero){
					var dataForm = layer.getChildFrame('form', index);
					dataForm.find("#submitBtn").click();
					return false
				}
			});    
		},moveTop:function(){
			var checkStatus = table.checkStatus('org-table')
			,checkData = checkStatus.data; 
			if(checkData.length === 0){
			return layer.msg('请选择要移动的机构',{icon:0});
			}
			layer.confirm("确定将【"+checkData[0].name+"】移动到顶部吗？",function(index){
				if( checkData[0].parentId==null || checkData[0].parentId=="" ){
					return layer.msg('已是顶级机构，不需要移动',{icon:0});
				}
				$.ajax({
					url: 'org/moveTop',
					data: {id: checkData[0].id},
					type:'POST',
					dataType:'JSON',
					success:function(ret){
						var currentNodes = treeObj.getNodesByParam("id", checkData[0].id, null);
						if(currentNodes.length==0){
							treeObj.reAsyncChildNodes(null, "refresh");
						}else{
							treeObj.moveNode(null,currentNodes[0], "inner");
							if(currentNodes[0].isParent){
								currentNodes[0].zAsync = false;
								currentNodes[0].id = ret.data.id;
								treeObj.updateNode(currentNodes[0]);
							}
						}
						
						layer.close(index);
					}
				})
			})
		}
		,move: function(){
			var checkStatus = table.checkStatus('org-table')
			,checkData = checkStatus.data; 
			if(checkData.length === 0){
			return layer.msg('请选择要移动的机构',{icon:0});
			}
			var movedId = checkData[0].id;
			var movedOrg = getOrgById(movedId);
			layer.open({
				type: 2,
				title:'请选择移动的目标机构',
				area:['400px','600px'],
				content:  'rooter?url=system/org/move-org',
				btn:['移动到该机构下','关闭'],
				yes:function(index,layero){
					
					var iframeWin = window[layero.find('iframe')[0]['name']];
					var destNodes = iframeWin.treeObj.getSelectedNodes();
					if(destNodes.length==0){
						return layer.msg("请选择目标机构",{icon:0});
					}
					var values = {id:checkData[0].id,parentId:destNodes[0].id};
					
					if(values.parentId==movedId){
						return layer.msg("不能将该机构移动到自己下面");
					}
					if(movedOrg.parentId!==null && movedOrg.parentId!="null"){
						if(movedOrg.parentId===destNodes[0].id){
							return layer.msg("现在就在【"+destNodes[0].name+"】下面，不需要移动");
						}
					}
					if((destNodes[0].code).indexOf(movedOrg.code)!=-1){
						return layer.msg("不能把机构移动到自己的下级机构");
					}
					$.ajax({
						url: 'org/move',
						data: values,
						type:'POST',
						dataType:'JSON',
						success:function(ret){
							layer.close(index);
							table.reload('org-table');
							var currentParentNodes = treeObj.getNodesByParam("id", values.parentId, null);
							if(movedOrg.parentId!==null && movedOrg.parentId!="null"){
								var oldParentNodes = treeObj.getNodesByParam("id", movedOrg.parentId, null);
								$.ajax({
									url: 'org/countNodes',
									data: {parentId: movedOrg.parentId},
									type:'POST',
									dataType:'JSON',
									success:function(ret){
										if(ret == 0){
											oldParentNodes[0].isParent = false;
										}
										treeObj.updateNode(oldParentNodes[0]);
									}
								})
								
							}
							var movedNodes = treeObj.getNodesByParam("id", values.id, null);
							if(currentParentNodes.length>0){
								if(movedNodes.length>0){
									treeObj.moveNode(currentParentNodes[0],movedNodes[0], "inner");
									movedNodes[0].parentId = currentParentNodes[0].id;
									movedNodes[0].id = ret.data.id
								}else{
									currentParentNodes[0].isParent = true;
									currentParentNodes[0].zAsync = false;
								}
								treeObj.updateNode(currentParentNodes[0]);
							}else{
								treeObj.removeNode(movedNodes[0]);
							}
							
							
						}
					})
				},
				success: function(layero, index){
					layer.title("将【"+checkData[0].name+"】移动到",index);
				}
			});    
		}
	};
	window.setSelectedRows=function(id){
		selectedRows(id,table,form,'org-table','org-table');
	}
	$('.layui-btn').each(function(i,obj){
		$(this).on('click', function(){
			var type = $(this).data('type');
			action[type].call(this);
		})
	});
	
	setTimeout(function(){
		treeObj.expandNode(treeObj.getNodesByParam("id","1",null)[0],true,true,true);  
	},100)
	
	});
	function openDetail(t,id){
		layer.open({
			type: 2,
			title:'修改公司信息',
			area:['1000px','700px'],
			content: 'rooter?url=system/org/add-org&id='+id,
			btn:['保存','关闭'],
			yes:function(index,layero){
				var dataForm = layer.getChildFrame('form', index);
				dataForm.find("#submitBtn").click();
				return false
			}
		});    
	}
</script>
</body>